package com.zhongzhi.thread;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import com.zhongzhi.common.Common;
import com.zhongzhi.common.Plc;
import com.zhongzhi.common.Primary;
import com.zhongzhi.springboot.pojo.Computers;
import com.zhongzhi.springboot.pojo.EquipmentShow;
import com.zhongzhi.springboot.pojo.Station;
import com.zhongzhi.springboot.pojo.StationShow;
import com.zhongzhi.util.ClientUtil;
import com.zhongzhi.util.ConfigUtil;
import com.zhongzhi.util.JsonResult;

import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TabPane;
import lombok.extern.log4j.Log4j;
@Log4j
public class PageThread extends Thread{
	
	@Override
	public void run() {
		getParameters();								//获取本机参数
		ServerTherad therad = new ServerTherad();		//打开连接服务器测试线程
		therad.start();
		selectCommonPlc();								//获取设备公共数据
		selectCommonPlcAsStation();						//查询本机设备对象以及设备下的工位
		selectCommonPlcAsStation2();					//根据plc地址集合查询工位信息
		//启动plc监控线程
		PlcGuardThread plcGuardThread = new PlcGuardThread();
		plcGuardThread.start();
		//启动工位业务线程
		TaskGuardTherad taskGuardTherad = new TaskGuardTherad();
		taskGuardTherad.start();


		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				try {
					ScrollPane sp = Common.getSp();
					TabPane load = FXMLLoader.load(getClass().getResource("/fxml/run/TABS.fxml"));
					Common.setTp(load);
					sp.setContent(load);
				} catch (IOException e1) {
					log.error("主画面:跳转到监控画面失败:"+e1);
				}
			}
		});

	}
	private void getParameters() {
		//获取本机参数
		ConfigUtil configUtil= new ConfigUtil();
		configUtil.getConfiguration();
	}
	private void selectCommonPlc() {
		/**
		 * 向公共对象写入全部的plc信息
		 */
		//发起http请求,获取plc集合

		try {
			String s = "http://127.0.0.1:8080/equipment/select?pages=1&line=255";
			log.info("主画面:发起http请求,获取plc集合:"+s);
			String gethttp = ClientUtil.Gethttp(s);
			log.info("主画面:发起http请求,获取plc集合结果:"+gethttp);
			JsonResult jsonobj = ClientUtil.Jsonobj(gethttp, JsonResult.class);
			ArrayList<EquipmentShow> eqlist = (ArrayList<EquipmentShow>) ClientUtil.Jsonlist(jsonobj.getData().toString(), EquipmentShow.class);
			Common.setEquipmentList(eqlist);
		} catch (Exception e) {
			log.error("主画面:发起http请求,获取plc集合失败:"+e);
		}
	}
	private void selectCommonPlcAsStation() {
		//读取所有计算机信息
		try {
			String s = "http://127.0.0.1:8080/computers/select?pages=1&line=200";
			log.info("主画面:发起http请求,获取计算机集合:"+s);
			String stationhttp = ClientUtil.Gethttp(s);
			log.info("主画面:发起http请求,获取计算机集合:"+stationhttp);
			JsonResult jsonobj = ClientUtil.Jsonobj(stationhttp, JsonResult.class);
			String string = jsonobj.getData().toString();
			List<Computers> jsonlist = ClientUtil.Jsonlist(string, Computers.class);
			Common.getArrayList().addAll(jsonlist);

		} catch (Exception e) {
			log.error("主画面:发起http请求,获取计算机信息失败:"+e);
		}



		/*
		 * 向公共对象写入本机的plc信息
		 */
		try {
			//1:根据本机站号动态生成查询语句
			ArrayList<Computers>  list = Common.getArrayList();
			if (list!=null && list.size()!=0) {
				String property = Common.getConfiguration().getProperty();
				String string = "http://127.0.0.1:8080/equipment/selectByIds?";
				int a = 0;
				for (Computers computers : list) {
					String computers2 = computers.getComputers();
					if (computers2!=null && computers2.equals("")!=true) {
						boolean equals = computers.getComputers().equals(property);
						if (equals) {
							if (a==0) {
								string = string + "ids=" + computers.getId();
							}else {
								string = string + "&ids=" + computers.getId();
							}
							a++;
						}
					}

				}

				//2:根据计算机id查询设备信息
				log.info("主画面:获取本机管理的设备:"+string);
				String stationhttp = ClientUtil.Gethttp(string);
				log.info("主画面:获取本机管理的设备结果:"+stationhttp);
				JsonResult jsonobj2 = ClientUtil.Jsonobj(stationhttp, JsonResult.class);
				String s = jsonobj2.getData().toString();
				if (s!=null && s.equals("")!=true) {
					List<EquipmentShow> equipmentlist = ClientUtil.Jsonlist(s, EquipmentShow.class);
					System.out.println(equipmentlist);
					HashMap<Integer, EquipmentShow> equipmentMap = Common.getEquipmentMap();
					a = 0;
					for (EquipmentShow equipmentShow : equipmentlist) {
						//把本机管理的设备存入公共类
						try {
							equipmentMap.put(equipmentShow.getId(), equipmentShow);
							String stationGet = "http://127.0.0.1:8080/station/selectByequipmentId?equipmentId="+equipmentShow.getId();
							log.info("主画面:根据plcid获取工位信息:"+stationGet);
							String b = ClientUtil.Gethttp(stationGet);
							log.info("主画面:根据plcid获取工位信息结果:"+b);
							JsonResult jsonobj3 = ClientUtil.Jsonobj(b, JsonResult.class);
							String string3 = jsonobj3.getData().toString();
							Plc plc = new Plc(equipmentShow);
							if (string3!=null && string3.equals("")!=true) {
								List<StationShow> jsonlist = ClientUtil.Jsonlist(string3, StationShow.class);
								for (StationShow sShow : jsonlist) {
									plc.getStTemData().put(sShow.getId(), sShow);
								}
							}
							Primary.getMap().put(equipmentShow.getId(), plc);
						} catch (Exception e) {
							log.error("主画面:根据plcid获取工位信息失败:"+e);
						}

					}
				}
			}
		} catch (Exception e) {
			log.error("主画面:获取本机管理的设备失败"+e);
		}

	}
	private void selectCommonPlcAsStation2() {
		//根据plc地址集合查询工位信息
		try {
			String abc = "http://127.0.0.1:8080/station/selectByequipmentIds?";

			HashMap<Integer, EquipmentShow> equipmentMap = Common.getEquipmentMap();
			if (equipmentMap!=null && equipmentMap.size()!=0) {
				int x = 0;
				Set<Integer> keySet = equipmentMap.keySet();
				for (Integer integer : keySet) {
					if (x==0) {
						abc=abc+"equipmentIds="+integer;
					}else {
						abc=abc+"&equipmentIds="+integer;
					}
					x++;
				}
				log.info("主画面:根据plc地址集合查询工位信息:"+abc);
				String stationjson  = ClientUtil.Gethttp(abc);
				log.info("主画面:根据plc地址集合查询工位信息结果:"+stationjson);
				JsonResult jsonobj2 = ClientUtil.Jsonobj(stationjson, JsonResult.class);

				String string = jsonobj2.getData().toString();
				if (string!=null && string.equals("")!=true ) {
					List<Station> stationList = ClientUtil.Jsonlist(string, Station.class);

					for (Station station : stationList) {
						Common.getTaskStationMap().put(station.getId(),station);
					}

				}

			}


		} catch (Exception e) {
			log.error("主画面:根据plc地址集合查询工位信息失败:"+e);
		}

	}

}
